package com.xyh.flowershop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyh.flowershop.constant.RequestBaseConstant;
import com.xyh.flowershop.service.FileService;
import com.xyh.flowershop.service.TypeService;
import com.xyh.flowershop.vo.resp.common.ResultRespVo;
import com.xyh.flowershop.vo.resp.type.TypePageRespVo;
import com.xyh.flowershop.vo.req.type.TypeEditReqVo;
import com.xyh.flowershop.vo.req.type.TypePageReqVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/type/")
@Tag(name = "分类信息接口")
@CrossOrigin(origins = "*", maxAge = 3600)
public class TypeController {
    private final TypeService typeService;

    private final FileService fileService;

    public TypeController(TypeService typeService, FileService fileService) {
        this.typeService = typeService;
        this.fileService = fileService;
    }

    @Operation(description = "类别的编辑或者新增接口")
    @PostMapping("editOrAdd")
    public ResultRespVo editOrAddType(@RequestBody TypeEditReqVo vo){
        boolean flag;
        if(vo.getTypeId() == null){
            flag = typeService.addType(vo);
        }else {
            flag = typeService.editType(vo);
        }
        return flag ? ResultRespVo.success() : ResultRespVo.error();
    }

    @Operation(description = "删除分类")
    @DeleteMapping("delete/{id}")
    public ResultRespVo deleteType(@PathVariable Long id){
        return typeService.deleteType(id) ?
                ResultRespVo.success()
                : ResultRespVo.error();
    }

    @Operation(description = "类别批量删除")
    @DeleteMapping("delete/batch")
    public ResultRespVo deleteBatchType(@RequestBody List<Long> ids){
        return typeService.deleteBatchType(ids) ?
                ResultRespVo.success()
                : ResultRespVo.error();
    }

    @Operation(description = "类别分页查询")
    @PostMapping("app/find/page")
    public ResultRespVo findPageType(@RequestBody TypePageReqVo vo){

       IPage<TypePageRespVo> vos =  typeService.getPageType(vo);

       return ResultRespVo.success(200, "查询成功", vos);
    }

    @Operation(description = "根据编号查找")
    @GetMapping("find/{id}")
    public ResultRespVo findType(@PathVariable Long id){
        return ResultRespVo.success(200, "处理成功", typeService.getType(id));
    }

    @Operation(description = "文件上传")
    @PostMapping("upload")
    public ResultRespVo uploadFile(@RequestBody MultipartFile file, HttpServletRequest request){
        String path = RequestBaseConstant.BASE_URL+"/img/type/"+fileService.uploadFile(file, request.getRequestURI());
        return ResultRespVo.success(path);
    }

    @GetMapping("app/find/typeKV")
    @Operation(description = "获得分类的键值对")
    public ResultRespVo findTypeKV(){
        return  ResultRespVo.success(typeService.selectTypeKV());
    }

}
